<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:应用组
 * @LastEditors: liuhongbin
 */

namespace app\adminapi\controller\v1\addonAuth;

use app\common\model\addon\Addons;
use hiduoke\services\FormBuilder as Form;
use think\App;
use think\Request;
use app\common\controller\AdminController;
use app\common\model\addonAuth\AddonGroup as AddonGroupModel;

class AddonGroup extends AdminController
{
    /**
     * @var AddonGroupModel
     */
    public $model;

    public function __construct(App $app, AddonGroupModel $model)
    {
        parent::__construct($app);
        $this->model = new $model;

    }

    /**
     * 显示资源列表
     * @Route("restful", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        $data = $this->model->getPageListData()->append(['explain']);
        $count = $this->model->getPageListCount();
        return $this->success($count, $data);
    }

    /**
     * 显示创建资源表单页.
     * @Route("restful/create", method="GET")
     * @return \think\Response
     */
    public function create()
    {
        $rule = array();
        //创建表单
        $form = Form::createForm("/adminapi/addonGroup/save")->setMethod("POST");
        //新增表单
        $rule[] = Form::input('name', '应用组名称', "")->required();
        $rule[] = Form::switches("default", "是否默认", '0')->info("如果设置为默认，则创建的新用户默认启用该配置");
        $rule[] = Form::dkTypeInput('type', '应用样式组', "primary");
        $rule[] = Form::dkAddonSelect('addons', '应用及权限', [])->appendToBody()->addonsExplain()->required();
        $form->setTitle("新增应用组")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存新建的资源
     * @Route("restful", method="POST")
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->post('formData', []);
        if (!$formData['name'] || !$formData['addons']) {
            return $this->error("名称和应用必选");
        }
        $this->model->save($formData);
        return $this->success("新增成功");

    }

    /**
     * 显示指定的资源
     * @Route("restful/id", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        return $this->success("read");

    }

    /**
     * 显示编辑资源表单页.
     * @Route("restful/id/edit", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $addonsModel = new Addons();
        $rule = array();
        $row = $this->model->where(['id' => $id])->findOrEmpty();
        $addonsExplain = $addonsModel->getAddonsByFormDataAddons($row['addons']);
        //创建表单
        $form = Form::createForm("/adminapi/addonGroup/update?id=" . $id)->setMethod("POST");
        //新增表单
        $rule[] = Form::input('name', '应用组名称', $row->name)->required();
        $rule[] = Form::switches("default", "是否默认", strval($row->default) ?? '0')->info("如果设置为默认，则创建的新用户默认启用该配置");
        $rule[] = Form::dkTypeInput('type', '应用样式组', $row->type ?: 'primary');
        $rule[] = Form::dkAddonSelect('addons', '应用及权限', $row->addons)->appendToBody()->addonsExplain($addonsExplain)->required();
        $form->setTitle("新增应用组")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存更新的资源
     * @Route("restful/id", method="PUT")
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $addon_group = (new AddonGroupModel())->where(['id' => $id])->find();
        if (!$addon_group) {
            return $this->error("应用组不存在");
        }
        $formData = $request->post('formData', []);
        if (!$formData['name'] || !$formData['addons']) {
            return $this->error("名称和应用必选");
        }
        if ($formData['default'] == "1") {
            (new AddonGroupModel())->where(['default' => 1])->whereNotIn('id', [$id])->save(['default' => 0]);
        }
        $addon_group->save($formData);
        return $this->success("编辑成功");
    }

    /**
     * 删除指定资源
     * @Route("restful/id", method="DELETE")
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $this->model->where(['id' => $id])->delete();
        return $this->success("删除成功");
    }
}
